local extension=Package("nshou_sx")
extension.extensionName="tshu"

local U = require "packages/utility/utility"

Fk:loadTranslationTable{
  ["nshou_sx"]="年兽.生肖",
  ["tshusx"] = "生肖",
}


local tu = General(extension, "tshusx__tu", "wei", 3, 3, General.Female)
tu.hidden = true
local ji = General(extension, "tshusx__ji", "wei", 3, 3, General.Male)
ji.hidden = true
local niu = General(extension, "tshusx__niu", "wei", 1, 5, General.Male)
niu.hidden = true

local she = General(extension, "tshusx__she", "shu", 3, 3, General.Female)
she.hidden = true
local long = General(extension, "tshusx__long", "shu", 4, 4, General.Male)
long.hidden = true
local gou = General(extension, "tshusx__gou", "shu", 4, 4, General.Male)
gou.hidden = true

local hou = General(extension, "tshusx__hou", "wu", 3, 3, General.Male)
hou.hidden = true
local hu = General(extension, "tshusx__hu", "wu", 4, 4, General.Male)
hu.hidden = true
local yang = General(extension, "tshusx__yang", "wu", 3, 3, General.Male)
yang.hidden = true

local ma = General(extension, "tshusx__ma", "qun", 4, 4, General.Male)
ma.hidden = true
local shu = General(extension, "tshusx__shu", "qun", 3, 3, General.Male)
shu.hidden = true
local zhu = General(extension, "tshusx__zhu", "qun", 5, 5, General.Male)
zhu.hidden = true







Fk:loadTranslationTable{
  ["tshusx__shu"] = "子鼠",
  ["~tshusx__shu"] = "油米全没了……",
  ["tshusx__niu"] = "丑牛",
  ["~tshusx__niu"] = "请将我埋于此地吧……",
  ["tshusx__hu"] = "寅虎",
  ["~tshusx__hu"] = "百兽之王，也有终老……",
  ["tshusx__tu"] = "卯兔",
  ["~tshusx__tu"] = "这灾祸……是躲不过了……",
  ["tshusx__long"] = "辰龙",
  ["~tshusx__long"] = "龙威不再，龙鳞已落……",
  ["tshusx__she"] = "巳蛇",
  ["~tshusx__she"] = "我的毒液……失效了……",
  ["tshusx__ma"] = "午马",
  ["~tshusx__ma"] = "马有失蹄啊……",
  ["tshusx__yang"] = "未羊",
  ["~tshusx__yang"] = "看不到青草翠绿时……",
  ["tshusx__hou"] = "申猴",
  ["~tshusx__hou"] = "这仙桃……无用了……",
  ["tshusx__ji"] = "酉鸡",
  ["~tshusx__ji"] = "杀鸡取卵，不可取呀。",
  ["tshusx__gou"] = "戌狗",
  ["~tshusx__gou"] = "不能守护家园了",
  ["tshusx__zhu"] = "亥猪",
  ["~tshusx__zhu"] = "噢，果然，还是吃太多了。",
}

local zishu = fk.CreateActiveSkill{
  name = "tshu__zishu",
  anim_type = "offensive",
  card_num = 0,
  target_num = 1,
  can_use = function(self, player)
    return player:usedSkillTimes(self.name, Player.HistoryPhase) == 0
  end,
  card_filter = Util.FalseFunc,
  target_filter = function(self, to_select, selected, selected_cards)
    local target = Fk:currentRoom():getPlayerById(to_select)
    return #selected == 0 and target:getHandcardNum() > Self:getHandcardNum()
  end,
  on_use = function(self, room, effect)
    local player = room:getPlayerById(effect.from)
    local target = room:getPlayerById(effect.tos[1])
    local c = room:askForCardChosen(player, target, "h", self.name)
    room:obtainCard(player, c, false, fk.ReasonPrey)
    local targets, tos = {}, {}
    while true do
      targets = table.filter(room:getOtherPlayers(player), function(p)
          return p:getHandcardNum() > player:getHandcardNum()
      end)
      if #targets == 0 then break end
      tos = player.room:askForChoosePlayers(player, table.map(targets, Util.IdMapper), 1, 1, "#tshu__zishu-choose", self.name, true)
      if #tos ~= 1 then break end
      c = room:askForCardChosen(player, room:getPlayerById(tos[1]), "h", self.name)
      room:obtainCard(player, c, false, fk.ReasonPrey)
    end
  end,
}
shu:addSkill(zishu)

Fk:loadTranslationTable{
  ["tshu__zishu"] = "子鼠",
  [":tshu__zishu"] = "出牌阶段限一次，获得一名手牌数大于你的角色一张手牌直到你的手牌数成为全场最大。",
  ["#tshu__zishu-choose"] = "子鼠：指定一名手牌数大于你的角色，获得其一张手牌。",
  ["$tshu__zishu"] = "这些牌都归我吧！"
}

local chouniu = fk.CreateTriggerSkill{
  name = "tshu__chouniu",
  anim_type = "support",
  events = {fk.EventPhaseStart},
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(self)
      and player.phase == Player.Finish
      and #table.filter(player.room:getOtherPlayers(player), function(p) return p.hp < player.hp end) == 0
  end,
  on_use = function(self, event, target, player, data)
    player.room:recover{
    who = player,
    num = 1,
    recoverBy = player,
    skillName = self.name
    }
  end,
}

niu:addSkill(chouniu)

Fk:loadTranslationTable{
  ["tshu__chouniu"] = "丑牛",
  [":tshu__chouniu"] = "锁定技，结束阶段，若你的体力值为全场最小，你回复1点体力。",
  ["$tshu__chouniu"] = "牛角之歌，自保足矣。"
}

local yinhu = fk.CreateActiveSkill{
  name = "tshu__yinhu",
  anim_type = "offensive",
  card_num = 1,
  target_num = 1,
  can_use = function(self, player)
    return not player:isNude() and player:getMark("disable_yinhu-turn") == 0
  end,
  card_filter = function(self, to_select, selected)
    if #selected == 0 and not Self:prohibitDiscard(Fk:getCardById(to_select)) then
      local mark = Self:getTableMark( "tshu__yinhu-phase")
      return not table.contains(mark, Fk:getCardById(to_select).type)
    end
  end,
  target_filter = function(self, to_select, selected)
    return to_select ~= Self.id
  end,
  on_use = function(self, room, effect)
    local player = room:getPlayerById(effect.from)
    local target = room:getPlayerById(effect.tos[1])
    local mark = player:getTableMark( "tshu__yinhu-phase")
    table.insert(mark, Fk:getCardById(effect.cards[1]).type)
    room:setPlayerMark(player, "tshu__yinhu-phase", mark)
    room:throwCard(effect.cards, self.name, player)
    if not target.dead then
      room:damage{
        from = player,
        to = target,
        damage = 1,
        skillName = self.name,
      }
    end
  end,
}
local yinhu_trigger = fk.CreateTriggerSkill{
  name = "#yinhu_trigger",
  mute = true,
  events = {fk.EnterDying},
  can_trigger = function(self, event, target, player, data)
    return data.damage and data.damage.skillName == "tshu__yinhu" and data.damage.from
  end,
  on_cost = Util.TrueFunc,
  on_use = function(self, event, target, player, data)
    player.room:setPlayerMark(player, "disable_yinhu-turn", 1)
  end,
}

yinhu:addRelatedSkill(yinhu_trigger)
hu:addSkill(yinhu)

Fk:loadTranslationTable{
  ["tshu__yinhu"] = "寅虎",
  [":tshu__yinhu"] = "出牌阶段每种花色的牌限一次，你可以弃置一张牌，对一名其他角色造成1点伤害。若其因此进入濒死状态，此技能失效直到回合结束。",
  ["#yinhu_trigger"] = "寅虎-失效",
  ["$tshu__yinhu"] = "尝尝我的厉害吧！",
}

local maotu = fk.CreateTriggerSkill{
  name = "tshu__maotu",
  anim_type = "special",
  frequency = Skill.Compulsory,
  events = {fk.Deathed},
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(self)
  end,
  on_use = function(self, event, target, player, data)
    player.room:setPlayerMark(player, "@@tshu__maotu", 1)
  end,

  refresh_events = {fk.EventPhaseStart, fk.Death},
  can_refresh = function(self, event, target, player, data)
    return target == player and player:getMark("@@tshu__maotu") ~= 0 and (event == fk.Death or player.phase == Player.Play)
  end,
  on_refresh = function(self, event, target, player, data)
    player.room:setPlayerMark(player, "@@tshu__maotu", 0)
  end,
}
local maotu_prohibit = fk.CreateProhibitSkill{
  name = "#tshu__maotu_prohibit",
  mute = true,
  frequency = Skill.Compulsory,
  is_prohibited = function(self, from, to, card)
    return to:hasSkill(self) and to:getMark("@@tshu__maotu") ~= 0 and from ~= to and from.hp >= to.hp
  end,
}

maotu:addRelatedSkill(maotu_prohibit)
tu:addSkill(maotu)

Fk:loadTranslationTable{
  ["tshu__maotu"] = "卯兔",
  [":tshu__maotu"] = "锁定技，其他角色死亡时，你不能成为其他角色使用牌的目标直到你的下个出牌阶段开始时。",
  ["@@tshu__maotu"] = "卯兔",
  ["$tshu__maotu"] = "想抓到我？不可能！",
}

local chenlong = fk.CreateActiveSkill{
  name = "tshu__chenlong",
  anim_type = "offensive",
  frequency = Skill.Limited,
  target_num = 1,
  can_use = function(self, player)
    return player:usedSkillTimes(self.name, Player.HistoryGame) == 0
  end,
  interaction = function()
    return UI.Spin {
      from = 1,
      to = Self.hp,
    }
  end,
  card_filter = Util.FalseFunc,
  target_filter = function(self, to_select, selected, selected_cards)
    return to_select ~= Self.id and Fk:currentRoom():getPlayerById(to_select).role ~= Self.role
  end,
  on_use = function(self, room, effect)
    local player = room:getPlayerById(effect.from)
    local target = room:getPlayerById(effect.tos[1])
    room:loseHp(player, self.interaction.data, self.name)
    room:damage{
      from = player,
      to = target,
      damage = self.interaction.data,
      skillName = self.name,
    }
  end,
}
local chenlong_trigger = fk.CreateTriggerSkill{
  name = "#tshu__chenlong_trigger",
  anim_type = "support",
  frequency = Skill.Compulsory,
  events = {fk.EnterDying},
  can_trigger = function(self, event, target, player, data)
    local skillEvent = player.room.logic:getCurrentEvent().parent.parent
    if #skillEvent.data < 3 then return false end
    return skillEvent.data[3] == "tshu__chenlong" and target == player and player:hasSkill(self) and not data.damage and not target.dead and target.hp < 1
  end,
  on_use = function(self, event, target, player, data)
    player.room:recover{
      who = player,
      num = 1-player.hp,
      recoverBy = player,
      skillName = self.name
    }
    if not player.dead then
      player.room:changeMaxHp(player, -1)
    end
  end,
}

chenlong:addRelatedSkill(chenlong_trigger)
long:addSkill(chenlong)

Fk:loadTranslationTable{
  ["tshu__chenlong"] = "辰龙",
  [":tshu__chenlong"] = "限定技，你可以失去至多5点体力，对一名其他角色造成等量伤害。若你因此进入濒死状态，你回复体力至1点，减1点体力上限。",
  ["#tshu__chenlong_trigger"] = "辰龙-回复",
  ["$tshu__chenlong"] = "龙怒的威力，不是你所能承受的。",
}

local sishe = fk.CreateTriggerSkill{
  name = "tshu__sishe",
  anim_type = "masochism",
  events = {fk.Damaged},
  can_trigger = function(self, event, target, player, data)
    return data.to == player and data.to:hasSkill(self) and data.from and not data.from.dead
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local from = data.from
    if from and not from.dead
      then room:doIndicate(player.id, {from.id})
    end
    room:damage{
      from = player,
      to = from,
      damage = data.damage,
      skillName = self.name,
    }
  end
}

she:addSkill(sishe)

Fk:loadTranslationTable{
  ["tshu__sishe"] = "巳蛇",
  [":tshu__sishe"] = "当你受到伤害后，你可以对伤害来源造成等量伤害。",
  ["$tshu__sishe"] = "伤我者，一一奉还。",
}

local wuma = fk.CreateTriggerSkill{
  name = "tshu__wuma",
  mute = true,
  frequency = Skill.Compulsory,
  events = {fk.BeforeTurnOver, fk.EventPhaseSkipping, fk.TargetConfirmed},
  can_trigger = function(self, event, target, player, data)
    if player:hasSkill(self) and target == player then
      return (event == fk.TargetConfirmed and data.card.type == Card.TypeTrick) or event ~= fk.TargetConfirmed
    end
  end,
  on_use = function(self, event, target, player, data)
    if event == fk.TargetConfirmed then
      player.room:notifySkillInvoked(player, self.name, "drawcard")
      player:broadcastSkillInvoke(self.name)
      player:drawCards(1, self.name)
    else
      player.room:notifySkillInvoked(player, self.name, "defensive")
      player:broadcastSkillInvoke(self.name)
      return true
    end
  end,
}

ma:addSkill(wuma)

Fk:loadTranslationTable{
  ["tshu__wuma"] = "午马",
  [":tshu__wuma"] = "锁定技，你翻面前，取消之；你的阶段被跳过前，取消之；当你成为其他角色锦囊牌的目标后，你摸一张牌。",
  ["$tshu__wuma"] = "有我在，必成功！",
}

local weiyang = fk.CreateActiveSkill{
  name = "tshu__weiyang",
  anim_type = "support",
  can_use = function(self, player)
    return player:usedSkillTimes(self.name, Player.HistoryPhase) == 0
  end,
  card_filter = function(self, to_select, selected, targets)
    if #selected == 0 then return
      not Self:prohibitDiscard(Fk:getCardById(to_select))
    else return
      table.every(selected, function(id) return Fk:getCardById(to_select):getTypeString() ~= Fk:getCardById(id):getTypeString() end)
      and not Self:prohibitDiscard(Fk:getCardById(to_select))
    end
  end,
  target_filter = function(self, to_select, selected, cards)
    return #selected < #cards and Fk:currentRoom():getPlayerById(to_select):isWounded()
  end,
  on_use = function(self, room, effect)
    local from = room:getPlayerById(effect.from)
    room:throwCard(effect.cards, self.name, from, from)
    if from:isAlive() and from:isWounded() then
      for _, id in ipairs(effect.tos) do
        room:recover({
          who = room:getPlayerById(id),
          num = 1,
          recoverBy = from,
          skillName = self.name
        })
      end
    end
  end,
}

yang:addSkill(weiyang)

Fk:loadTranslationTable{
  ["tshu__weiyang"] = "未羊",
  [":tshu__weiyang"] = "出牌阶段限一次，你可以弃置任意张类别不同的牌，令至多等量角色各回复1点体力。",
  ["$tshu__weiyang"] = "共享绵泽，同甘共苦。",
}

local shenhou = fk.CreateTriggerSkill{
  name = "tshu__shenhou",
  frequency = Skill.Compulsory,
  events = {fk.TargetConfirmed},
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(self) and data.card.trueName == "slash"
  end,
  on_use = function(self, event, target, player, data)
    player.room:notifySkillInvoked(player, self.name, "defensive")
    local judge = {
      who = player,
      reason = self.name,
      pattern = ".|.|heart,diamond",
    }
    player.room:judge(judge)
    if judge.card.color == Card.Red then
      table.insertIfNeed(data.nullifiedTargets, player.id)
      return true
    end
  end,
}

hou:addSkill(shenhou)

Fk:loadTranslationTable{
  ["tshu__shenhou"] = "申猴",
  [":tshu__shenhou"] = "当你成为【杀】的目标后，你可以判定。若结果为红色，你令之对你无效。",
  ["$tshu__shenhou"] = "百般变化，真假难辨！",
}

local youji = fk.CreateTriggerSkill{
  name = "tshu__youji",
  anim_type = "drawcard",
  frequency = Skill.Compulsory,
  events = {fk.DrawNCards},
  on_use = function(self, event, target, player, data)
    data.n = data.n + math.min(player.room:getTag("RoundCount"), 5)
  end,
}

ji:addSkill(youji)

Fk:loadTranslationTable{
  ["tshu__youji"] = "酉鸡",
  [":tshu__youji"] = "锁定技，摸牌阶段，你多摸X张牌（X为游戏轮数，至多为5）。",
  ["$tshu__youji"] = "鸡豚之息，虽微渐厚！",
}

local xugou = fk.CreateTriggerSkill{
  name = "tshu__xugou",
  mute = true,
  frequency = Skill.Compulsory,
  events = {fk.TargetConfirmed, fk.AfterCardUseDeclared},
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(self) and data.card.trueName == "slash" and data.card.color == Card.Red
  end,
  on_use = function(self, event, target, player, data)
    if event == fk.TargetConfirmed then
      player.room:notifySkillInvoked(player, self.name, "defensive")
      player:broadcastSkillInvoke(self.name)
      table.insertIfNeed(data.nullifiedTargets, player.id)
    else
      player.room:notifySkillInvoked(player, self.name, "offensive")
      player:broadcastSkillInvoke(self.name)
      data.additionalDamage = (data.additionalDamage or 0) + 1
    end
  end,
}
local xugou_targetmod = fk.CreateTargetModSkill{
  name = "#xugou_targetmod",
  frequency = Skill.Compulsory,
  residue_func = function(self, player, skill, scope)
    if player:hasSkill(self) and skill.trueName == "slash_skill" then
      return 1
    end
  end,
}

xugou:addRelatedSkill(xugou_targetmod)
gou:addSkill(xugou)

Fk:loadTranslationTable{
  ["tshu__xugou"] = "戌狗",
  [":tshu__xugou"] = "锁定技，红色【杀】对你无效；你使用的红色【杀】无距离限制且造成的伤害+1。",
  ["$tshu__xugou"] = "驱邪吠恶，遇凶斩杀。",
}

local haizhu = fk.CreateTriggerSkill{
  name = "tshu__haizhu",
  mute = true,
  frequency = Skill.Compulsory,
  events = {fk.AfterCardsMove, fk.EventPhaseStart},
  can_trigger = function(self, event, target, player, data)
    if player:hasSkill(self) then
      if event == fk.AfterCardsMove then
        local ids = {}
        local room = player.room
        for _, move in ipairs(data) do
          if move.toArea == Card.DiscardPile then
            if move.moveReason == fk.ReasonDiscard and move.from and move.from ~= player.id then
              for _, info in ipairs(move.moveInfo) do
                if (info.fromArea == Card.PlayerHand or info.fromArea == Card.PlayerEquip) and
                Fk:getCardById(info.cardId).color == Card.Black and
                room:getCardArea(info.cardId) == Card.DiscardPile then
                  table.insertIfNeed(ids, info.cardId)
                end
              end
            end
          end
        end
        ids = U.moveCardsHoldingAreaCheck(room, ids)
        if #ids > 0 then
          self.cost_data = ids
          return true
        end
      else return
        target == player and player.phase == Player.Start and
        table.every(player.room:getOtherPlayers(player), function(p) return p:getHandcardNum() <= player:getHandcardNum() end)
      end
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    if event == fk.AfterCardsMove then
      player.room:notifySkillInvoked(player, self.name, "drawcard")
      player:broadcastSkillInvoke(self.name)
      local ids = table.simpleClone(self.cost_data)
      if #ids > 0 then
        room:moveCardTo(ids, Card.PlayerHand, player, fk.ReasonPrey, self.name)
      end
    else
      player.room:notifySkillInvoked(player, self.name, "negtive")
      room:loseHp(player, 1, self.name)
    end
  end,
}

zhu:addSkill(haizhu)

Fk:loadTranslationTable{
  ["tshu__haizhu"] = "亥猪",
  [":tshu__haizhu"] = "锁定技，其他角色的黑色牌因弃置进入弃牌堆后，你获得之；准备阶段，若你的手牌数为全场最大，你失去1点体力。",
  ["$tshu__haizhu"] = "这些都归我吧。",
}


return extension
